package com.xboard.server.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.xboard.server.common.exception.DataAccessException;
import com.xboard.server.dao.UserDao;
import com.xboard.server.manager.DataManager;

/**
 * Handles requests for the application home page.
 */
@Repository
public class UserDaoImpl implements UserDao{

	@Autowired
	private DataManager dataManager;
	private String TableName = "User";  // JDO table name
	
	public UserDaoImpl(){
		
	}
	
	@Override
	public void addUser(Map<String, Object> user){
		try {
		    Key customerKey = KeyFactory.createKey(TableName, UUID.randomUUID().toString());
		    Date date = new Date();
		    Entity en = getEntity(user.get("id").toString());
		    if(en!=null && en.hasProperty("id")){
		    	updateUser(user);
		    }else{
			    Entity enty = new Entity(TableName, customerKey);			
				enty.setProperty("id", user.get("id"));
				enty.setProperty("name", user.get("name"));
				enty.setProperty("email", user.get("email"));
				enty.setProperty("date", date);
				dataManager.insert(enty);
		    }
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	@Override
	public List<Map<String,Object>> listUser(){
		Query query = new Query(TableName).addSort("date", Query.SortDirection.DESCENDING);
		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		try {
			list = dataManager.list(query);

		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return list;
	}
	@Override
	public Map<String,Object> getUser(String id){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			
			Entity enty = getEntity(id);
			
			result.put("id", enty.getProperty("id"));
			result.put("name", enty.getProperty("name"));
			result.put("email", enty.getProperty("email"));
			result.put("date", enty.getProperty("date"));
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return result;
	}	
	
	@Override
	public void updateUser(Map<String, Object> user){
		try {
			String id = user.get("id").toString();
			Date date = new Date();
			
			Entity enty = getEntity(id);			
			enty.setProperty("name", user.get("name"));
			enty.setProperty("email", user.get("email"));
			enty.setProperty("date", date);
			
			System.out.println("update : "+ getKey(id).toString());
			
			dataManager.update(enty);	
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	
	@Override
	public void deleteUser(String id){
		try {
			dataManager.delete(getEntity(id));	
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	
	@Override
	public Key getKey(String id){
		Key key = null;
		try {
	        Query query = new Query(TableName);
			query.addFilter("id", FilterOperator.EQUAL, id);
			
			key = dataManager.getKey(query);
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return key;
	}	
	@Override
	public Entity getEntity(String id){
		Entity enUser = null;
		try {
	        Query query = new Query(TableName);
			query.addFilter("id", FilterOperator.EQUAL, id);
			enUser = dataManager.getEntity(query);
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return enUser;
	}	
}
